package edu.sysu.cshare.control;

import java.io.IOException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import edu.sysu.cshare.model.Comment;
import edu.sysu.cshare.model.Message;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.model.Message.Category;
import edu.sysu.cshare.util.ContinueFTP;
import edu.sysu.cshare.util.HibernateSessionFactory;

public class MessageManager {

	public MessageManager(User actor) {
		super();
		this.actor = actor;
	}

	public static Message getMessageById(long id) {
		Session ss = HibernateSessionFactory.getSession();
		return (Message) ss.load(Message.class, id);
	}

	public static Comment getCommentById(long id) {
		return (Comment) HibernateSessionFactory.getSession().load(
				Comment.class, id);
	}

	User actor;

	public User getActor() {
		return actor;
	}

	public void announce(Message msg) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		msg.setPublisher(actor);
		ss.save(msg);
		ss.getTransaction().commit();
	}

	public void announce(Message msg, String attachLocalPath)
			throws IOException {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		ContinueFTP ftp = new ContinueFTP();
		ftp.connect();
		String fileName = attachLocalPath.substring(attachLocalPath
				.lastIndexOf("\\") + 1);
		ftp.upload(attachLocalPath, SystemManager.RemoteConfig.getMsgAttDir()
				+ fileName);

		msg.setAttachPath(SystemManager.RemoteConfig.getMsgAttDir() + fileName);
		msg.setPublisher(actor);
		ss.save(msg);
		ss.getTransaction().commit();
	}

	/**
	 * 
	 * @param msg
	 *            持久化的Msg对象(有id)
	 * @param comment
	 *            评论内容
	 */
	public void comment(Message msg, String content) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		Comment cmt = new Comment(this.actor, content, msg);
		msg.getComments().add(cmt);
		ss.save(cmt);
		ss.saveOrUpdate(msg);
		ss.getTransaction().commit();

	}

	public void delete(Message msg) {
		Session ss = HibernateSessionFactory.getSession();
		ss.beginTransaction();
		ss.delete(msg);
		ss.getTransaction().commit();
	}

	/**
	 * @deprecated
	 * @param msgId
	 */
	public void commentbyId(String msgId) {

	}

	public List<Message> getAllMessages() {
		Session ss = HibernateSessionFactory.getSession();
		return ss.createQuery("from Message m order by m.createDate desc")
				.list();

	}

	/*public List<Message> getMyMessages() {
		Session ss = HibernateSessionFactory.getSession();
		return ss.createQuery("from Message m where m.publisher=:user")
				.setEntity("user", actor).list();

	}

	public List<Comment> getMyComments() {
		Session ss = HibernateSessionFactory.getSession();
		return ss.createQuery("from Comment c where c.commenter=:user")
				.setEntity("user", actor).list();

	}*/

	public List<Message> getByCategory(Category category) {
		Session ss = HibernateSessionFactory.getSession();
		if (category == Category.All) {
			List<Message> msgs = ss.createQuery(
					"from Message m order by m.createDate desc").list();
			return msgs;
		} else {
			return ss
					.createQuery(
							"from Message m where m.category =? order by m.createDate desc")
					.setParameter(0, category).list();
		}
	}

	/**
	 * 
	 * @param category
	 *            类别 若是ALL则返回全部类别
	 * @param pageNumber
	 *            从0开始计数的 指定的页数
	 * @param pageSize
	 *            指定页的大小
	 * @return 超出范围返回空列表
	 */
	public List<Message> getSelected(Category category, int pageNumber,
			int pageSize) {
		Session ss = HibernateSessionFactory.getSession();
		if (category == Category.All) {
			return ss.createQuery("from Message m order by m.createDate desc")
					.setMaxResults(pageSize)
					.setFirstResult(pageNumber * pageSize).list();
		} else {
			return ss
					.createQuery(
							"from Message m where m.category =? order by m.createDate desc")
					.setParameter(0, category).setMaxResults(pageSize)
					.setFirstResult(pageNumber * pageSize).list();
		}
	}

	public void downloadAttach(Message message) {
		//TODO
		System.out.println("download attach" +message);
	}

}
